﻿Imports System.Net
Imports System.IO

Public Class clsParseHTML
    Public Function getByAttribute(ByVal html As String, ByVal tag As String, ByVal attr As String) As ArrayList
        Dim web As New WebBrowser
        Dim arr As New ArrayList
        web.Navigate("about:blank")
        web.Document.Write("<html><head><title></title><body>" & html & "</body></html>")
        Dim PageElement As HtmlElementCollection = web.Document.GetElementsByTagName(tag)
        For Each CurElement As HtmlElement In PageElement
            If (attr = "innerText") Then
                arr.Add(CurElement.InnerText)
            ElseIf attr = "innerHTML then" Then
                arr.Add(CurElement.InnerHtml)
            Else
                arr.Add(CurElement.GetAttribute(attr))
            End If

        Next
        Return arr
    End Function

    Public Function getByClassname(ByVal html As String, ByVal tag As String, ByVal classname As String, Optional ByVal innerText As Boolean = False) As ArrayList
        Dim web As New WebBrowser
        Dim arr As New ArrayList
        web.Navigate("about:blank")
        web.Document.Write("<html><head><title></title><body>" & html & "</body></html>")
        Dim PageElement As HtmlElementCollection = web.Document.GetElementsByTagName(tag)
        For Each CurElement As HtmlElement In PageElement
            If (CurElement.GetAttribute("className") = classname) Then
                If (innerText = True) Then
                    arr.Add(CurElement.InnerText)
                Else
                    arr.Add(CurElement.InnerHtml)
                End If
            End If
        Next
        Return arr
    End Function

    Public Function getByName(ByVal html As String, ByVal tag As String, ByVal name As String, ByVal attr As String, Optional ByVal innerText As Boolean = False) As ArrayList
        Dim web As New WebBrowser
        Dim arr As New ArrayList
        web.Navigate("about:blank")
        web.Document.Write("<html><head><title></title><body>" & html & "</body></html>")
        Dim PageElement As HtmlElementCollection = web.Document.GetElementsByTagName(tag)
        For Each CurElement As HtmlElement In PageElement
            If CurElement.GetAttribute("name") = name Then
                If (attr = "innerText") Then
                    arr.Add(CurElement.InnerText)
                ElseIf attr = "innerHTML then" Then
                    arr.Add(CurElement.InnerHtml)
                Else
                    arr.Add(CurElement.GetAttribute(attr))
                End If
            End If
        Next
        Return arr
    End Function

    Public Function CheckByName(ByRef web As WebBrowser, ByVal tag As String, ByVal name As String)
        Dim t = web.Document.GetElementsByTagName(tag)
        If (t.Count > 0) Then
            t = t.GetElementsByName(name)
            If (t.Count > 0) Then
                Return True
            End If
        End If
        Return False
    End Function

    Public Sub SetByName(ByRef web As WebBrowser, ByVal tag As String, ByVal name As String, ByVal key As String, ByVal value As String)
        If CheckByName(web, tag, name) = True Then
            If key = "innerText" Then
                Debug.Print(key & " - " & value)
                web.Document.GetElementsByTagName(tag).GetElementsByName(name).Item(0).InnerText = value
            Else
                Debug.Print(key & value)
                web.Document.GetElementsByTagName(tag).GetElementsByName(name).Item(0).SetAttribute(key, value)
            End If
        Else
            Debug.Print("Can't find " & name)
        End If
    End Sub

    Public Sub SetSubmitById(ByRef web As WebBrowser, ByVal id As String)
        If (web.Document IsNot Nothing) Then
            If (web.Document.GetElementById(id) IsNot Nothing) Then
                web.Document.GetElementById(id).InvokeMember("submit")
            End If
        End If
    End Sub


    Public Function getURL(ByVal url As String, Optional ByRef ErrorCallback As Action(Of String) = Nothing)
        Debug.Print(url)
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Try
            request = DirectCast(WebRequest.Create(url), HttpWebRequest)
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())
            Dim rawresp As String
            rawresp = reader.ReadToEnd()
            If rawresp <> "" Then
                Return rawresp
            End If
        Catch ex As Exception
            If (ErrorCallback IsNot Nothing) Then
                ErrorCallback("Error")
            End If
        Finally

        End Try
        Return ""
    End Function

    Function URLEncode(ByVal Text As String) As String
        Return Uri.EscapeUriString(Text)
    End Function

    Public Function getObjectByClassname(ByRef web As WebBrowser, ByVal tag As String, ByVal classname As String) As HtmlElement
        Dim PageElement As HtmlElementCollection = Web.Document.GetElementsByTagName(tag)
        For Each CurElement As HtmlElement In PageElement
            Debug.Print(CurElement.GetAttribute("className"))
            If (CurElement.GetAttribute("className") = classname) Then
                Return CurElement
            End If
        Next
        Return Nothing
    End Function


    Public Sub SetSubmitByClassname(ByRef web As WebBrowser, ByVal classname As String)
        If (web.Document IsNot Nothing) Then
            Dim form As HtmlElement = getObjectByClassname(web, "form", classname)
            If form IsNot Nothing Then
                form.InvokeMember("submit")
            Else
                Debug.Print("bug")
            End If
        End If
    End Sub
End Class
